package artid.com.arttogo.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SectionIndexer;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import java.util.List;
import artid.com.arttogo.R;
import artid.com.arttogo.activity.CollecterActivity;
import artid.com.arttogo.bean.ArtCollectorsNewParser;
import artid.com.arttogo.utils.MyBitmapImageViewTarget;
import artid.com.arttogo.utils.NetUtils;
import artid.com.arttogo.view.CircleImageView;
import artid.com.arttogo.view.alphabeticalindex.IndexesMechanismBean;
import artid.com.arttogo.view.horizonlistview.HorizonListviewNestingListView;
import artid.com.arttogo.view.horizonlistview.NewartistArtistHorizontalList_Adapter;

import static android.media.CamcorderProfile.get;

/**
 *
 * 类名称：IndexesArtist_Adapter
 * 类描述：首页艺术家字母索引适配器
 * 创建人：耿卫斌
 * 创建时间：2017/5/2 14:23
 *
 */
public class IndexesArtist_Adapter extends BaseAdapter implements SectionIndexer {
    private Context mContext;
    private List<IndexesMechanismBean> list = null;
    private List<ArtCollectorsNewParser> list_ArtCollectorsNewParser;
    public IndexesArtist_Adapter(Context mContext, List<IndexesMechanismBean> list, List<ArtCollectorsNewParser> list_ArtCollectorsNewParser) {
        this.mContext = mContext;
        this.list = list;
        this.list_ArtCollectorsNewParser = list_ArtCollectorsNewParser;
    }

    /**
     * 当ListView数据发生变化时,调用此方法来更新ListView
     * @param list
     */
    public void updateListView(List<IndexesMechanismBean> list){
        this.list = list;
        notifyDataSetChanged();
    }

    public int getCount() {
        return this.list.size();
    }

    public Object getItem(int position) {
        return list.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(final int position, View view, ViewGroup arg2) {
        ViewHolder viewHolder = null;
        final IndexesMechanismBean mContent = list.get(position);
        final ArtCollectorsNewParser bean=list_ArtCollectorsNewParser.get(list.get(position).getSubscript());
        if (view == null) {
            viewHolder = new ViewHolder();
            view = LayoutInflater.from(mContext).inflate(R.layout.item_indexesartist, null);
            viewHolder.indexesartist_RL= (RelativeLayout) view.findViewById(R.id.item_indexesartist_RL);
            viewHolder.tvLetter = (TextView) view.findViewById(R.id.item_indexesartist_catalog);
            viewHolder.head_img = (CircleImageView) view.findViewById(R.id.item_indexesartist_head);
            viewHolder.name = (TextView) view.findViewById(R.id.item_indexesartist_name);
            viewHolder.worksNum = (TextView) view.findViewById(R.id.item_indexesartist_worksNum);
            viewHolder.followNum = (TextView) view.findViewById(R.id.item_indexesartist_followNum);
            viewHolder.follow = (ImageView) view.findViewById(R.id.item_indexesartist_follow);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        //根据position获取分类的首字母的Char ascii值
        int section = getSectionForPosition(position);

        //如果当前位置等于该分类首字母的Char的位置 ，则认为是第一次出现
        if(position == getPositionForSection(section)){
            viewHolder.tvLetter.setVisibility(View.VISIBLE);
            viewHolder.tvLetter.setText(mContent.getSortLetters());
        }else{
            viewHolder.tvLetter.setVisibility(View.GONE);
        }
        Glide.with(mContext).load(bean.getArtistimg()
                + "_" + (Integer.parseInt(NetUtils.getDisplayMetrics((Activity) mContext).get(NetUtils.DM_WIDTH)) / 3) + "x10.png").asBitmap().centerCrop()
                .placeholder(R.drawable.head_sq).error(R.drawable.head_sq)
                .into(new MyBitmapImageViewTarget(viewHolder.head_img));
        viewHolder.indexesartist_RL.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(mContext, CollecterActivity.class);
                intent.putExtra("name", bean.getRealname());
                intent.putExtra("id", bean.getArtist_id());
                mContext.startActivity(intent);
            }
        });
        viewHolder.name.setText(bean.getRealname());
        viewHolder.worksNum.setText(bean.getArtNum() + "件作品");
        viewHolder.followNum.setText(bean.getFollowNum() + "人关注");
        if (bean.getIs_follow() == 0) {
            //未关注
            viewHolder.follow.setImageResource(R.drawable.ysj_btn_guanzhu);
        } else if (bean.getIs_follow() == 1) {
            //已关注
            viewHolder.follow.setImageResource(R.drawable.ysj_btn_guanzhu_sel);
        }
        viewHolder.head_img.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(mContext, CollecterActivity.class);
                intent.putExtra("id", bean.getArtist_id());
                mContext.startActivity(intent);
            }
        });
        return view;

    }


    static class ViewHolder {
        RelativeLayout indexesartist_RL;
        TextView tvLetter;
        CircleImageView head_img;
        ImageView follow;
        TextView name, worksNum, followNum;
    }


    /**
     * 根据ListView的当前位置获取分类的首字母的Char ascii值
     */
    public int getSectionForPosition(int position) {
        return list.get(position).getSortLetters().charAt(0);
    }

    /**
     * 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置
     */
    public int getPositionForSection(int section) {
        for (int i = 0; i < getCount(); i++) {
            String sortStr = list.get(i).getSortLetters();
            char firstChar = sortStr.toUpperCase().charAt(0);
            if (firstChar == section) {
                return i;
            }
        }

        return -1;
    }

    /**
     * 提取英文的首字母，非英文字母用#代替。
     *
     * @param str
     * @return
     */
    private String getAlpha(String str) {
        String sortStr = str.trim().substring(0, 1).toUpperCase();
        // 正则表达式，判断首字母是否是英文字母
        if (sortStr.matches("[A-Z]")) {
            return sortStr;
        } else {
            return "#";
        }
    }

    @Override
    public Object[] getSections() {
        return null;
    }
}
